package com.t2c.tibetantochinese.common;

import android.util.Log;

import com.android.volley.Request;
import com.android.volley.Response;

import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.net.URLEncoder;

/**
 * Created by zoulu on 16/3/2.
 * 网络请求管理器
 */


public class HttpRequestManager {

    protected static HttpRequestManager sInstance;

    public static String baseUrl = "http://110.249.221.196:8080"; //测试环境

    public static String downloadURL = baseUrl + "/static/app.apk";

    public static HttpRequestManager getInstance(){
        if (sInstance == null) {
            sInstance = new HttpRequestManager();
        }
        return sInstance;
    }

    //汉藏翻译500句列表
    public HttpRequest fiveHundredList(Response.Listener<String> listener, Response.ErrorListener errorListener){
        String url = String.format("%s/getCategories", baseUrl );
        Log.e("=======", "fiveHundredListItem: "+url );
        return new HttpRequest(Request.Method.GET, url, null, listener, errorListener);
    }

    //对应的某一项
    public HttpRequest fiveHundredListItem(Response.Listener<String> listener, Response.ErrorListener errorListener,int index){
        String url = String.format("%s/getPhraseByCategory", baseUrl );
        url = url + "?categoryIndex=" + index ;
        Log.e("=======", "fiveHundredListItem: "+url );
        return new HttpRequest(Request.Method.GET, url, null, listener, errorListener);
    }
    //搜索
    public HttpRequest search(Response.Listener<String> listener, Response.ErrorListener errorListener,String str){
        String url = String.format("%s/searchByCategotyStr", baseUrl );
        try {
            str = URLEncoder.encode(str,"UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        url = url + "?categoryStr=" + str ;
        Log.e("=======", "fiveHundredListItem: "+url );
        return new HttpRequest(Request.Method.GET, url, null, listener, errorListener);
    }

    //登录
    public HttpRequest login(Response.Listener<String> listener, Response.ErrorListener errorListener,String name,String pass){
        String url = String.format("%s/doLogin", baseUrl );
        url = url + "?uid=" + name + "&upass=" +pass ;
        return new HttpRequest(Request.Method.GET, url, null, listener, errorListener);
    }

    //注册
    public HttpRequest register(Response.Listener<String> listener, Response.ErrorListener errorListener,String name,String pass){
        String url = String.format("%s/doRegist", baseUrl );
        url = url + "?uid=" + name + "&upass=" +pass ;
        return new HttpRequest(Request.Method.GET, url, null, listener, errorListener);
    }

    //获取藏文字学习列表
    public HttpRequest wordStudyList(Response.Listener<String> listener, Response.ErrorListener errorListener){
        String url = String.format("%s/getLearnDic", baseUrl );
        return new HttpRequest(Request.Method.GET, url, null, listener, errorListener);
    }

    //获取藏文字学习详情
    public HttpRequest wordStudyDetail(Response.Listener<String> listener, Response.ErrorListener errorListener,int uuid){
        String url = String.format("%s/getLearnDicById", baseUrl );
        url = url + "?id=" + uuid;
        return new HttpRequest(Request.Method.GET, url, null, listener, errorListener);
    }

    //搜索翻译获取 一页返回20个 page从1开始
    public HttpRequest translateGet(Response.Listener<String> listener, Response.ErrorListener errorListener,int page){
        String url = String.format("%s/getTrans", baseUrl );
        url = url + "?page=" + page;
        return new HttpRequest(Request.Method.GET, url, null, listener, errorListener);
    }

    //搜索翻译、电子词典搜索  page从1开始
    public HttpRequest translateAndDicSearch(Response.Listener<String> listener, Response.ErrorListener errorListener,String content,int page, int count){
        String url = String.format("%s/searchtran", baseUrl );
        url = url + "?content=" + content + "&page=" +page + "&count=" + count;
        return new HttpRequest(Request.Method.GET, url, null, listener, errorListener);
    }


    //电子词典获取  page从1开始
    public HttpRequest dictionaryGet(Response.Listener<String> listener, Response.ErrorListener errorListener,int page){
        String url = String.format("%s/getEDic", baseUrl );
        url = url + "?page=" + page;
        return new HttpRequest(Request.Method.GET, url, null, listener, errorListener);
    }

    //发送反馈
    public HttpRequest sendFeedBack(Response.Listener<String> listener, Response.ErrorListener errorListener,String name,String content){
        String url = String.format("%s/feedback ", baseUrl );
        String c = "";
        try {
            c = URLEncoder.encode(content,"UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        url = url + "?uid=" + name + "&content=" + c;
        return new HttpRequest(Request.Method.GET, url, null, listener, errorListener);
    }

    //获取开机画面
    public HttpRequest getADimg(Response.Listener<String> listener, Response.ErrorListener errorListener){
        String url = String.format("%s/getAdimg ", baseUrl );
        return new HttpRequest(Request.Method.GET, url, null, listener, errorListener);
    }

}
